package com.wuwei.aliocr;

import com.wuwei.aliocr.domain.Question;
import com.wuwei.aliocr.mapper.QuestionMapper;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.protocol.HTTP;
import com.wuwei.aliocr.utils.HttpUtils;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;

@SpringBootTest
public class JiWuZaiXian {

    private   Map<String,String> header;

    private  List<String> keys;

    private   Map<String,String> body;

    @SneakyThrows
    @Test
    public void getHead() {
        this.header = new HashMap<>();
        this.keys = new ArrayList<>();
        String fileName = "C:\\Users\\84206\\Desktop\\机务在线破解\\header.txt";
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        while (line!=null){
//            System.out.println(line);
            if (line.length() > 1) {
                int i = line.indexOf(":");
//                System.out.println(line.substring(0,i));
                this.keys.add(line.substring(0,i));
//                System.out.println(line.substring(i+2));
                this.header.put(line.substring(0,i),line.substring(i + 2));
            }
            line = bufferedReader.readLine();
        }
    }

    @SneakyThrows
    @Test
    public void getBody(){
        body = new HashMap<>();
        String fileName = "C:\\Users\\84206\\Desktop\\机务在线破解\\body.txt";
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        String[] split = line.split("&");
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            int i1 = s.indexOf("=");
            String key = s.substring(0, i1);
            String values = s.substring(i1+1);
            this.body.put(key,values);
        }
    }


    @Test
    public void timeTask(){
        Timer timer = new Timer();

        TimerTask task = new TimerTask() {
            int count = 1;

            @Override
            public void run() {
                System.out.println(count);
                count++;

                // 在这里添加你要执行的任务逻辑

                // 如果达到某个条件，你也可以调用 timer.cancel() 来停止定时器
            }
        };

        // 开始定时器，每隔20秒执行一次任务
        timer.schedule(task, 0, 20000);
    }


    @Test
    public  void  test1() throws Exception {
        this.getHead();
        this.getBody();
        Map<String,String> query = new HashMap<>();
        query.put("a","ajax");
        HttpResponse post = HttpUtils.doPost("https://www.airacm.com", "/learning.php", "POST", header, query, body);
        HttpEntity entity = post.getEntity();
        if (post.containsHeader(HTTP.CONTENT_ENCODING)) {
            entity = new GzipDecompressingEntity(entity);  // Gzip解压缩
        }
        if (entity != null) {
            String responseString = EntityUtils.toString(entity);
            this.deCodeHtml(responseString);
            // 处理响应体内容
        }
    }

    @SneakyThrows
    @Test
    public void getHtml(){
        String fileName = "C:\\Users\\84206\\Desktop\\h1.txt";
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        String html = "";
        while (line!=null){
            html +=line;
            line = bufferedReader.readLine();
        }
        this.deCodeHtml(html);
    }

    @Resource
    QuestionMapper questionMapper;

    /**
     * 提取html里的文字
     * @param responseString
     */
    @Test
    public void deCodeHtml(String responseString) {
        List<Question> list = new ArrayList<>();
        // 解析HTML文档
        Document doc = Jsoup.parse(responseString);
        Elements h1 = doc.select("h1");
        for (Element element : h1) {
            String text = element.text();
            Question question = new Question();
            question.setQuestion(text);
            list.add(question);
        }
        Elements li = doc.select("li");
        for (int i = 0; i < li.size(); i++) {
            Element element = li.get(i);
            String text1 = element.text();
            int a = i % 4;
            int b = i / 4;
            if (a==0) {
                list.get(b).setItem1(text1);
            } else if (a == 1) {
                list.get(b).setItem2(text1);
            } else if (a == 2) {
                list.get(b).setItem3(text1);
            } else {
                list.get(b).setItem4(text1);
            }
        }

        Elements span = doc.select("span.ui-images-maxwidth-limit");
        for (int i = 0; i < span.size(); i++) {
            Element element = span.get(i);
            String text = element.text();
            list.get(i).setRemark(text);
        }

        for (Question question : list) {
            String item1 = question.getItem1();
            String item2 = question.getItem2();
            String item3 = question.getItem3();
            String item4 = question.getItem4();
            String answer = "";
            if (item1.endsWith("◎") || item1.endsWith("√")) {
                answer += item1.charAt(0);
            }
            if (item2.endsWith("◎") || item2.endsWith("√")) {
                if ("".equals(answer)) {
                    answer += item2.charAt(0);
                } else {
                    answer += "+" + item2.charAt(0);
                }
            }
            if (item3.endsWith("◎") || item3.endsWith("√")) {
                if ("".equals(answer)) {
                    answer += item3.charAt(0);
                } else {
                    answer += "+" + item3.charAt(0);
                }
            }
            if (item4.endsWith("◎") || item4.endsWith("√")) {
                if ("".equals(answer)) {
                    answer += item4.charAt(0);
                } else {
                    answer += "+" + item4.charAt(0);
                }
            }
            question.setAnswer(answer);
            String question1 = question.getQuestion();
            int indexOf = question1.indexOf(".");
            if (indexOf < 4) {
                String substring = question1.substring(indexOf + 1);
                question.setQuestion(substring);
            }
            questionMapper.insert(question);
        }
    }
}
